<template>
  <div class="banner">
    <div class="box">
      <div
        class="img"
        :mouseover="stop()"
        :mouseout="move()"
        v-for="(item, index) in imgs"
        v-show="index === cur"
        :key="index"
      >
        <img :src="require('../' + item)" alt="" />
      </div>
      <div class="btn">
        <span class="left" @click="left">  </span>
        <span class="right" @click="right"> > </span>
      </div>
      <div class="num">
        <li
          v-for="(ele, i) in num"
          :key="i"
          @click="change(i)"
          :class="cur == i ? 'active' : ''"
        >
          {{ ele }}
        </li>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      num: ["1", "2", "3"],
      imgs: ["images/a1.jpg", "images/a2.jpg", "images/a3.jpg"],
      cur: 0,
      time: "",
    };
  },

  methods: {
    change(i) {
      this.cur = i;
    },
    left() {
      this.cur--;
      if (this.cur < 0) {
        this.cur = 2;
      }
    },
    right() {
      this.cur++;
      if (this.cur == 3) {
        this.cur = 0;
      }
    },
    play() {
      this.time = setInterval(() => {
        this.cur++;
        if (this.cur == 3) {
          this.cur = 0;
        }
      }, 2000);
    },
    stop() {
      clearInterval(this.time);
    },
    move() {
      this.play();
    },
  },
  created() {
    this.play();
  },
};
</script>

<style lang="scss" scoped>
.banner {
  width: 1200px;
  height: 700px;
  background-color: #000;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: auto;
  margin-top: 60px;
}
.box {
  width: 900px;
  height: 600px;
  background-color: aquamarine;
  position: relative;
}
li {
  list-style: none;
}
.img {
  width: 900px;
  height: 600px;
  background-color: #fff;

  img {
    width: 100%;
    height: 100%;
  }
}
.num {
  display: flex;
  position: absolute;
  bottom: -35px;
  right: 100px;
  li {
    width: 50px;
    height: 50px;
    color: #000;
    border-radius: 50%;
    background-color: #fff;
    text-align: center;
    line-height: 50px;
    font-size: 20px;
    margin: 10px;
    opacity: 0.2;
    font-weight: 900;
    font-size: 28px;
  }
  .active {
    opacity: 1;
  }
}
.btn {
  color: #fff;
  span {
    width: 50px;
    height: 30px;
    font-size: 20px;
    font-weight: bold;
    background-color: #000;
    text-align: center;
  }
  .left {
    position: absolute;
    left: 10px;
    bottom: 130px;
  }
  .right {
    position: absolute;
    right: 10px;
    bottom: 130px;
  }
}
//动画
.image-enter-active {
  transition: all 1s linear;
}
.image-leave-active {
  transition: all 1s linear;
}
.image-enter-to {
  transform: translateX(0);
}
.image-enter {
  transform: translateX(100%);
}
.image-leave {
  transform: translateX(0);
}
.image-leave-to {
  transform: translateX(-100%);
}
</style>